package com.firesys;


import com.firesys.dbhelper.SharedPreferenceUtil;
import com.firesys.dbhelper.XmlNameHelper;
import com.firesys.lgoin.ui.LoginActivity;
import com.firesys.rounting.ui.DeviceTaskActivity;
import com.firesys.setting.ui.SettingsActivity;


import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.view.Display;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TextView;
import android.app.TabActivity;

/**
 * 程序进入主界面
 * 
 * @author Administrator
 * 
 */
@SuppressWarnings("deprecation")
public class MainActivity extends TabActivity implements OnClickListener {
    
	public static MainActivity instance = null;
	public static String TAB_TAG_LOGIN = "login";
	public static String TAB_TAG_ROUTING = "routing";
	public static String TAB_TAG_OUTFIRE = "outfire";
	public static String TAB_TAG_SETTING = "setting";

	public static TabHost mTabHost;
	static final int COLOR1 = Color.parseColor("#787878");
	static final int COLOR2 = Color.parseColor("#ffffff");

	private ImageView mTabImg;// 动画图片

	private int zero = 0;// 动画图片偏移量
	private int currIndex = R.id.main_layout_login;// 当前页卡编号
	private int one;// 单个水平动画位移
	private int two;
	private int three;
	// 切换按钮
	ImageView mBut1, mBut2, mBut3, mBut4;
	TextView mCateText1, mCateText2, mCateText3, mCateText4;

	// 切换时跳转
	Intent mLoginItent, mRoutingIntent, mOutfireIntent, mSettingsIntent;

	// 切换动作样式
	private Animation left_in, left_out;
	private Animation right_in, right_out;
	public SharedPreferenceUtil  sp;
	


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		instance=this;
		sp=new SharedPreferenceUtil(this);
		String gpsdate = sp.getValue(XmlNameHelper.GPSDATE);
		if (gpsdate == null || gpsdate.equals("")) {
		    sp.insertValue(XmlNameHelper.GPSDATE, "0");
		    sp.insertValue(XmlNameHelper.GPSLATITUDE, "0.0");
		    sp.insertValue(XmlNameHelper.GPSLONGITUDE, "0.0");
		}
		if(TextUtils.isEmpty(sp.getValue(XmlNameHelper.IMEI))){
			//  获取手机IMEI号，并保存
				TelephonyManager tm = (TelephonyManager) getSystemService(MainActivity.TELEPHONY_SERVICE);
				sp.insertValue(XmlNameHelper.IMEI, tm.getDeviceId());
				//sp.insertValue(XmlNameHelper.IMEI, tm.getSubscriberId());
			}
		 Display currDisplay = getWindowManager().getDefaultDisplay();//获取屏幕当前分辨率
	        int displayWidth = currDisplay.getWidth();
	        int displayHeight = currDisplay.getHeight();
	        one = displayWidth/4; //设置水平动画平移大小
	        two = one*2;
	        three = one*3;
		prepareAnim();
		prepareIntent();
		prepareView();
		setupIntent();
		
	}

	/**
	 * 初始化切换动作
	 */
	private void prepareAnim() {
		left_in = AnimationUtils.loadAnimation(this, R.anim.left_in);
		left_out = AnimationUtils.loadAnimation(this, R.anim.left_out);

		right_in = AnimationUtils.loadAnimation(this, R.anim.right_in);
		right_out = AnimationUtils.loadAnimation(this, R.anim.right_out);
	}

	/**
	 * 获取基本控件
	 */
	private void prepareView() {
		mBut1 = (ImageView) findViewById(R.id.main_img_login);
		mBut2 = (ImageView) findViewById(R.id.main_img_routing);
		mBut3 = (ImageView) findViewById(R.id.main_img_outfire);
		mBut4 = (ImageView) findViewById(R.id.main_img_settings);
		mTabImg = (ImageView) findViewById(R.id.img_tab_now);
		findViewById(R.id.main_layout_login).setOnClickListener(this);
		findViewById(R.id.main_layout_routing).setOnClickListener(this);
		findViewById(R.id.main_layout_outfire).setOnClickListener(this);
		findViewById(R.id.main_layout_settings).setOnClickListener(this);

	}

	/**
	 * 初始化跳转
	 */
	private void prepareIntent() {
		mLoginItent = new Intent(this, LoginActivity.class);
		mRoutingIntent=new Intent(this, DeviceTaskActivity.class);
		mOutfireIntent=new Intent(this, LoginActivity.class);
		mSettingsIntent=new Intent(this, SettingsActivity.class);
	}

	// 为TabHost 添加选项,不特殊指定默认添加的第一个选项卡为首页
	private void setupIntent() {
		mTabHost = getTabHost();
		mTabHost.addTab(buildTabSpec(TAB_TAG_LOGIN, R.string.category_home,
				R.drawable.tab_weixin_normal, mLoginItent));
		mTabHost.addTab(buildTabSpec(TAB_TAG_ROUTING,
				R.string.category_channel, R.drawable.tab_find_frd_normal,
				mRoutingIntent));
		mTabHost.addTab(buildTabSpec(TAB_TAG_OUTFIRE, R.string.category_search,
				R.drawable.tab_address_normal, mOutfireIntent));
		mTabHost.addTab(buildTabSpec(TAB_TAG_SETTING,
				R.string.category_account, R.drawable.tab_settings_normal,
				mSettingsIntent));

	}

	// 创建选项卡，每个选项卡都有一个选项卡指示符,内容和tag标签用于跟踪
	private TabHost.TabSpec buildTabSpec(String tag, int resLabel, int resIcon,
			final Intent content) {
		return mTabHost
				.newTabSpec(tag)
				.setIndicator(getString(resLabel),
						getResources().getDrawable(resIcon))
				.setContent(content);
	}

	@Override
	public void onClick(View v) {
		Animation animation = null;
		
		switch (v.getId()) {
		case R.id.main_layout_login:
			
			mBut1.setImageDrawable(getResources().getDrawable(
					R.drawable.tab_weixin_pressed));
			
			if (currIndex == R.id.main_layout_routing) {
				animation = new TranslateAnimation(one, 0, 0, 0);
				mBut2.setImageDrawable(getResources().getDrawable(
						R.drawable.tab_address_normal));
			} else if (currIndex == R.id.main_layout_outfire) {
				animation = new TranslateAnimation(two, 0, 0, 0);
				mBut3.setImageDrawable(getResources().getDrawable(
						R.drawable.tab_find_frd_normal));
			} else if (currIndex == R.id.main_layout_settings) {
				animation = new TranslateAnimation(three, 0, 0, 0);
				mBut4.setImageDrawable(getResources().getDrawable(
						R.drawable.tab_settings_normal));
			}
			mTabHost.setCurrentTabByTag(TAB_TAG_LOGIN);
			break;
		case R.id.main_layout_routing:
			System.out.println("-------巡检-----");
			mBut2.setImageDrawable(getResources().getDrawable(
					R.drawable.tab_find_frd_pressed));
			mTabHost.setCurrentTabByTag(TAB_TAG_ROUTING);
			if (currIndex == R.id.main_layout_login) {
				animation = new TranslateAnimation(zero, one, 0, 0);
				mBut1.setImageDrawable(getResources().getDrawable(
						R.drawable.tab_weixin_normal));
			} else if (currIndex == R.id.main_layout_outfire) {
				animation = new TranslateAnimation(two, one, 0, 0);
				mBut3.setImageDrawable(getResources().getDrawable(
						R.drawable.tab_find_frd_normal));
			} else if (currIndex == R.id.main_layout_settings) {
				animation = new TranslateAnimation(three, one, 0, 0);
				mBut4.setImageDrawable(getResources().getDrawable(
						R.drawable.tab_settings_normal));
			}
			break;
		case R.id.main_layout_outfire:
			System.out.println("-------灭火-----");
			mBut3.setImageDrawable(getResources().getDrawable(
					R.drawable.tab_find_frd_pressed));
			mTabHost.setCurrentTabByTag(TAB_TAG_OUTFIRE);
			if (currIndex == R.id.main_layout_login) {
				animation = new TranslateAnimation(zero, two, 0, 0);
				mBut1.setImageDrawable(getResources().getDrawable(
						R.drawable.tab_weixin_normal));
			} else if (currIndex == R.id.main_layout_routing) {
				animation = new TranslateAnimation(one, two, 0, 0);
				mBut2.setImageDrawable(getResources().getDrawable(
						R.drawable.tab_address_normal));
			} else if (currIndex == R.id.main_layout_settings) {
				animation = new TranslateAnimation(three, two, 0, 0);
				mBut4.setImageDrawable(getResources().getDrawable(
						R.drawable.tab_settings_normal));
			}
			break;
		case R.id.main_layout_settings:
			System.out.println("-------设置-----");
			mTabHost.setCurrentTabByTag(TAB_TAG_SETTING);
			mBut4.setImageDrawable(getResources().getDrawable(
					R.drawable.tab_settings_pressed));
			if (currIndex == R.id.main_layout_login) {
				animation = new TranslateAnimation(zero, three, 0, 0);
				mBut1.setImageDrawable(getResources().getDrawable(
						R.drawable.tab_weixin_normal));
			} else if (currIndex == R.id.main_layout_routing) {
				animation = new TranslateAnimation(one, three, 0, 0);
				mBut2.setImageDrawable(getResources().getDrawable(
						R.drawable.tab_address_normal));
			} else if (currIndex == R.id.main_layout_outfire) {
				animation = new TranslateAnimation(two, three, 0, 0);
				mBut3.setImageDrawable(getResources().getDrawable(
						R.drawable.tab_find_frd_normal));
			}
			break;

		default:
			break;
		}
		System.out.println(animation==null);
		if(animation==null){
			return;
		}
		System.out.println("---------"+currIndex);
		currIndex = v.getId();
		animation.setFillAfter(true);// True:图片停在动画结束位置
		animation.setDuration(150);
		mTabImg.startAnimation(animation);
	}

	@Override
	public boolean dispatchKeyEvent(KeyEvent event) {
		if(event.getKeyCode()==KeyEvent.KEYCODE_BACK){
	      if(event.getAction()==KeyEvent.ACTION_DOWN&&event.getRepeatCount()==0){
	    	  Intent  intent=new Intent(MainActivity.this, ExitApp.class);
	    	  startActivity(intent);
	      }
	      return true;
		}
		return super.dispatchKeyEvent(event);
	}


	

}
